with Ada.Text_IO;			use Ada.Text_IO;

with Ada.Command_Line;		use Ada.Command_Line;

with Ada.Strings;			use Ada.Strings;

with GNAT.Sockets;          use GNAT.Sockets;

with Ada.Strings.Unbounded;		use Ada.Strings.Unbounded;

with Ada.Streams;
use type Ada.Streams.Stream_Element_Count;

procedure main is
	function httprequest (
		i : in String;
		p : in Port_Type; 
		r : in String
		) 
		return Unbounded_String is
	client  : socket_type;
	address : sock_addr_type;
	channel : stream_access;
	send    : String := (1 => ASCII.CR, 2 => ASCII.LF, 
						 3 => ASCII.CR, 4 => ASCII.LF);
	buffer  : Ada.Streams.Stream_Element_Array (1 .. 256);
	length  : Ada.Streams.Stream_Element_Offset;
	output  : Unbounded_String;
	begin
		GNAT.Sockets.Initialize;
		create_socket(client);
		address.addr := inet_addr(i);
		address.port := p;
		connect_socket(client, address);
		channel := stream(client);
		string'write(channel, "GET " & r & " HTTP/1.1" & send);
		loop
			Ada.Streams.Read (Channel.All, buffer, length);
			exit when length = 0;
			for I in 1 .. length loop
				append(output, Character'val(buffer(I)));
			end loop; -- for loop
		end loop;
		return output;
	end httprequest;

begin
	if Argument_Count < 3 then
		put_line("usage: <program> <ip address> <port> <page>");
		return;
	end if;
	parameters:
		declare 
			ip   : string := Argument(1);
			port : string := Argument(2); 
			page : string := Argument(3);
			received : Unbounded_String;
		begin
			received := httprequest(ip, port_type'value(port), page);
			put(to_string(received));
		end parameters;
end main;
